# ===----------------------------------------------------------------------=== #
# Copyright (c) 2025, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #

TEMPLATE = """\
# This file is generated by rules_pycross.
# It is not intended for manual editing.
\"\"\"Pycross-generated dependency targets.\"\"\"

load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@module_versions//:config.bzl", "PYTHON_VERSIONS_DOTTED")
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
load("@@rules_pycross+//pycross:defs.bzl", "pycross_wheel_build", "pycross_wheel_library")

_TESTONLY_DEPS = [
    "accelerate",
    "einx",
    "frozendict",
    "hypothesis",
    "librosa",
    "lm-eval",
    "mteb",
    "peft",
    "pygame",
    "reference_residual_fsq",
    "sentence-transformers",
    "soxr",
    "timm",
    "torchmetrics",
    "torchvision",
    "zhconv",
]

PINS = {{
{pins}
}}

# buildifier: disable=unnamed-macro
def targets():
    \"\"\"Generated package targets.\"\"\"

    for pin_name, pin_target in PINS.items():
        native.alias(
            name = pin_name,
            actual = ":" + pin_target,
            testonly = pin_name in _TESTONLY_DEPS,
        )

    native.alias(
        name = "torch@multiple",
        actual = select({{
            "@@//:amd_gpu": ":torch@2.8.0+rocm7.0.0.git64359f59",
            "@@//:nvidia_gpu": ":torch@2.8.0+cu128",
            "@platforms//os:macos": ":torch@2.8.0",
            "//conditions:default": ":torch@2.8.0+cpu",
        }}),
    )

    native.alias(
        name = "torchaudio@multiple",
        actual = select({{
            "@@//:amd_gpu": ":torchaudio@2.8.0+rocm7.0.0.git6e1c7fe9",
            "@@//:nvidia_gpu": ":torchaudio@2.8.0+cu128",
            "@platforms//os:macos": ":torchaudio@2.8.0",
            "@@//:linux_aarch64": ":torchaudio@2.8.0",
            "//conditions:default": ":torchaudio@2.8.0+cpu",
        }}),
    )

    native.alias(
        name = "torchvision@multiple",
        testonly = True,
        actual = select({{
            "@@//:amd_gpu": ":torchvision@0.23.0+rocm7.0.0.git824e8c87",
            "@@//:nvidia_gpu": ":torchvision@0.23.0+cu128",
            "@platforms//os:macos": ":torchvision@0.23.0",
            "@@//:linux_aarch64": ":torchvision@0.23.0",
            "//conditions:default": ":torchvision@0.23.0+cpu",
        }}),
    )

    extra_build_args = {{
        "copts": ["-fvisibility=default", "-w"],
        "linkopts": select({{
            "@platforms//os:linux": ["-Wl,-z,undefs"],
            "@platforms//os:macos": ["-Wl,-undefined,dynamic_lookup"],
        }}),
        "tags": [
            "manual",
            "requires-network",
        ],
        "exec_properties": {{"dockerNetwork": "bridge"}},
    }}

    build_targets = {{}}
    for version in PYTHON_VERSIONS_DOTTED:
        for platform in ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "x86_64-unknown-linux-gnu"]:
            alias_name = "_env_python_{{}}_{{}}".format(version, platform)
            build_targets[":" + alias_name] = "@@rules_pycross++environments+rules_pycross_all_environments//:python_{{}}_{{}}.json".format(version, platform)
            native.alias(
                name = alias_name,
                actual = "@@rules_pycross++environments+rules_pycross_all_environments//:python_{{}}_{{}}_config".format(version, platform),
            )

        for gpu in ["nvidia", "amd"]:
            selects.config_setting_group(
                name = "_env_python_{{}}_x86_64-unknown-linux-gnu_{{}}_gpu".format(version, gpu),
                match_all = [
                    "@@//:{{}}_gpu".format(gpu),
                    "_env_python_{{}}_x86_64-unknown-linux-gnu".format(version),
                ],
            )

    # buildifier: disable=unused-variable
    _target = select(build_targets)

{targets}def repositories():
    \"\"\"Generated package repositories.\"\"\"
{repositories}
"""
